#include "Particle.h"
#include "ParticleFlyweight.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"


CParticle::CParticle(void)
{
	m_bIsDead = false;
	m_fCurrLife = 0.0f;
	m_pFlyweight = nullptr;


}

CParticle::CParticle(float totalLife, float posX, float posY, CParticleFlyweight* flyweight)
{
	/*m_pFlyweight = flyweight;*/
	InitializeParticle(totalLife,posX,posY,flyweight);
	/*m_bIsDead = false;
	m_fCurrLife = 0.0f;
	
	m_fTotalLife = totalLife;
	m_fPosX = posX; 
	m_fPosY = posY; 

	auto FLW =  m_pFlyweight;
	m_fVelX = FLW->m_fMinVelX;
	m_fVelY = FLW->m_fMinVelX;
	m_fColA=  FLW->m_fMinColorA;
	m_fColR = FLW->m_fMinColorR;
	m_fColG = FLW->m_fMinColorG;
	m_fColB = FLW->m_fMinColorB;
	m_fRotation = FLW->m_fMinRotation;
	m_fScaleX = FLW->m_fMinScaleX;
	m_fScaleY = FLW->m_fMinScaleY;

	m_fGravX = FLW->m_fGravityX;
	m_fGravY = FLW->m_fGravityY;

	CalcChangePerSecond();*/

}

CParticle::~CParticle(void)
{
}

void CParticle::Update( float fElapsedTime )
{
	m_fCurrLife += fElapsedTime;
	if (m_fCurrLife >= m_fTotalLife)
		m_bIsDead = true;

	m_fVelX +=		m_fCPSVelX * fElapsedTime;
	m_fVelY +=		m_fCPSVelY * fElapsedTime;
	m_fColA +=		m_fCPSColA	* fElapsedTime;
	m_fColR +=		m_fCPSColR   * fElapsedTime;
	m_fColG +=		m_fCPSColG   * fElapsedTime;
	m_fColB +=		m_fCPSColB   * fElapsedTime;
	m_fRotation +=	m_fCPSRot   * fElapsedTime;
	m_fScaleX +=	m_fCPSScaleX * fElapsedTime;
	m_fScaleY +=	m_fCPSScaleY  * fElapsedTime;


	m_fPosX += (m_fVelX * fElapsedTime); 
	m_fPosY += (m_fVelY * fElapsedTime);

}
void CParticle::Render()
{
	if(m_bIsDead == false)
	{
		auto txtman = CSGD_TextureManager::GetInstance();
		int imgId = m_pFlyweight->m_nParticleImageID;
		
		int width = txtman->GetTextureWidth(imgId);
		int offsetX = (width/2);
		int height = txtman->GetTextureHeight(imgId);
		int offsetY = (height/2);

		float rotX = (float)width/2;
		float rotY = (float)height/2;

	
		
		D3DXCOLOR ColorInfo(m_fColR,m_fColG,m_fColB,m_fColA);
		CSGD_TextureManager::GetInstance()->Draw(m_pFlyweight->m_nParticleImageID,((int)PosX()-offsetX),((int)PosY()-offsetY),m_fScaleX,m_fScaleY,0,rotX,rotY,m_fRotation,ColorInfo);
		 
	}
}

void CParticle::CalcChangePerSecond()
{
	auto flw = Flyweight();
	m_fCPSScaleX =	(flw->MaxScaleX() - flw->MinScaleX())/m_fTotalLife;
	m_fCPSScaleY =	(flw->MaxScaleY() - flw->MinScaleY())/m_fTotalLife;
	m_fCPSColA	 =	(flw->MaxColorA() - flw->MinColorA())/m_fTotalLife;
	m_fCPSColR	 =	(flw->MaxColorR() - flw->MinColorR())/m_fTotalLife;
	m_fCPSColG	 =	(flw->MaxColorG() - flw->MinColorG())/m_fTotalLife;
	m_fCPSColB	 =	(flw->MaxColorB() - flw->MinColorB())/m_fTotalLife;
	m_fCPSRot	 =	(flw->MaxRotation() - flw->MinRotation())/m_fTotalLife;
	m_fCPSVelX	 =	(flw->MaxVelX() - flw->MinVelX())/m_fTotalLife;
	m_fCPSVelY	 =	(flw->MaxVelY() - flw->MinVelY())/m_fTotalLife;

}

void CParticle::InitializeParticle(float totalLife, float posX, float posY, CParticleFlyweight* flyweight)
{
	m_bIsDead = false;
	m_fCurrLife = 0.0f;
	m_pFlyweight = flyweight;
	m_fTotalLife = totalLife;
	m_fPosX = posX; 
	m_fPosY = posY; 

	auto FLW =  m_pFlyweight;
	m_fVelX = FLW->m_fMinVelX;
	m_fVelY = FLW->m_fMinVelX;
	m_fColA=  FLW->m_fMinColorA;
	m_fColR = FLW->m_fMinColorR;
	m_fColG = FLW->m_fMinColorG;
	m_fColB = FLW->m_fMinColorB;
	m_fRotation = FLW->m_fMinRotation;
	m_fScaleX = FLW->m_fMinScaleX;
	m_fScaleY = FLW->m_fMinScaleY;

	m_fGravX = FLW->m_fGravityX;
	m_fGravY = FLW->m_fGravityY;

	CalcChangePerSecond();
}